package com.hck.trigger.http;

import com.hck.api.model.*;
import com.hck.domain.user.service.IUserService;
import com.hck.types.model.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/api/user")
@Tag(name = "用户接口", description = "用户相关操作")
public class UserController {

    @Resource
    private IUserService userService;

    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "新用户注册")
    public Response<UserRegisterResponseDTO> register(@RequestBody UserRegisterRequestDTO requestDTO) {
        UserRegisterResponseDTO responseDTO = userService.register(requestDTO);

        if (responseDTO.getSuccess()) {
            return Response.<UserRegisterResponseDTO>builder()
                    .code("200")
                    .data(responseDTO)
                    .info("注册成功")
                    .build();
        }

        return Response.<UserRegisterResponseDTO>builder()
                    .code("400")
                    .data(responseDTO)
                    .info(responseDTO.getErrorMessage())
                    .build();

    }

    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "通过用户名和密码登录")
    public Response<UserLoginResponseDTO> login(@RequestBody UserLoginRequestDTO requestDTO) {
        UserLoginResponseDTO responseDTO = userService.login(requestDTO);

        if (responseDTO.getSuccess()) {
            return Response.<UserLoginResponseDTO>builder()
                    .code("200")
                    .data(responseDTO)
                    .info("登录成功")
                    .build();
        }

        return Response.<UserLoginResponseDTO>builder()
                    .code("400")
                    .data(responseDTO)
                    .info(responseDTO.getErrorMessage())
                    .build();

    }

    @GetMapping("/info/{userId}")
    @Operation(summary = "获取用户信息", description = "根据用户ID获取用户详细信息")
    public Response<UserInfoDTO> getUserInfo(@PathVariable Long userId) {
        UserInfoDTO userInfo = userService.getUserInfo(userId);
        
        if (userInfo != null) {
            return Response.<UserInfoDTO>builder()
                    .code("200")
                    .data(userInfo)
                    .info("获取成功")
                    .build();
        } else {
            return Response.<UserInfoDTO>builder()
                    .code("404")
                    .data(null)
                    .info("用户不存在")
                    .build();
        }
    }


    @PostMapping("/authentic")
    @Operation(summary = "用户密码认证",description = "修改个人信息的密码认证")
    public Response<Boolean> authentic(@RequestBody AuthenticateRequestDTO authenticateRequestDTO){
        boolean result =  userService.authentic(authenticateRequestDTO);
        if(result)
            return Response.<Boolean>builder()
                    .code("200")
                    .info("认证成功")
                    .data(true)
                    .build();


        return Response.<Boolean>builder()
                .code("400")
                .info("认证失败")
                .data(false)
                .build();
    }


    @PostMapping("/updatePassword")
    @Operation(summary = "个人密码修改",description = "密码修改")
    public Response<Boolean> updatePassword(@RequestBody UpdatePasswordRequestDTO requestDTO){
        boolean result = userService.updatePassword(requestDTO);
        if(result)
            return Response.<Boolean>builder()
                    .code("200")
                    .info("修改密码成功，请重新登录")
                    .data(true)
                    .build();


        return Response.<Boolean>builder()
                .code("400")
                .info("原密码错误，请重试")
                .data(false)
                .build();
    }

    @PostMapping("/updateInfo")
    @Operation(summary = "个人信息修改",description = "信息修改")
    public Response<Boolean> update(@RequestBody UpdateInfoDTO dto){
        log.info("请求参数：" + dto.toString());
        userService.update(dto);
        return  Response.<Boolean>builder()
                .code("200")
                .data(true)
                .info("修改成功")
                .build();
    }

}